<?php

class AskModel extends Model {
	
	var $tableName = 'ask_context';
	/**
	 * 获取问题列表
	 *
	 * @param $uid string
	 *       	 空:所有问题;uid:用户的提问
	 * @param $flag int
	 *       	 标识符 1=正常；0=关闭；-1=删除
	 * @param $sort int
	 *       	 获取推荐度值
	 *       	
	 */
	function listAsk($uid = '', $flag = 1, $sort = '', $cid=-1) {
		$page = intval ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		$num = intval ( $_GET ['size'] ) ? $_GET ['size'] : 10;
		
		$cacheName = getCacheName ( "ask@model:listask/page:" . $page . ";uid:{$uid};flag:{$flag};" );
		$data = S ( $cacheName );
		if (! $data) {
			$askContextModel = M ( 'ask_context' );
			$askUploadModel = M ( 'ask_upload' );
			$where = '1';
			if ($uid) {
				$where .= ' and user_id=' . intval ( $uid );
			}
			if ($flag == - 1) {
				$where .= " and ask_flag=-1 ";
			} elseif ($flag == 0) {
				$where .= " and ask_flag=0 ";
			} else {
				$where .= " and ask_flag=1";
			}
			$_sort = 'ask_id desc';
			/*if ($sort == 1) {
				$where .= " and ask_sort=1";
				$_sort .= ',rand()';
			} elseif ($sort == 0 && $sort != '') {
				$where .= " and ask_sort=0";
			}*/
			// == 1 to be resolved
			if ($sort == 2) {
				// == 2 high credit
				$_sort = 'ask_pay desc';
			} elseif ($sort == 3) {
				// ==3 my question only
				$where .= " and user_id=" . $uid;
			}
			
			if ($cid>-1) {
				$where .= " and ask_cid=" . intval($cid);
			}
			$data ['item'] = $askContextModel->where ( $where )->order ( $_sort )->page ( $page . ',' . $num )->findAll ();
			// 获取图片
			foreach ( ( array ) $data ['item'] as $key => $val ) {
				$upload = $askUploadModel->where ( 'context_id=' . intval ( $val ['ask_id'] ) )->find ();
				if ($upload ['upload_name']) {
					$data ['item'] [$key] ['thumbName'] = $upload ['upload_name'];
					// 获取缩略图
					$data ['item'] [$key] ['thumb'] = $this->_thumb ( $upload ['upload_path'], $upload ['upload_name'] );
				}
				$commentModel = M ( 'comment' );
				$commentWhere = "appid=" . intval ( $val ['ask_id'] );
				$data ['item'] [$key] ['ask_num'] = $commentModel->where ( $commentWhere )->count ();
			}
			$data ['total'] = $askContextModel->where ( $where )->count ();
			
			// 分页
			import ( "@.ORG.Page" );
			// $page = new AskPage ( $data ['total'], $num );
			$page = new page ( $data ['total'], $num );
			$data ['pageShow'] = $page->show ();
			S ( $cacheName, $data, C ( 'cache_time_mini' ) );
		}
		return $data;
	}
	
	function search($key, $flag = 1) {
		$page = intval ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		
		$cacheName = getCacheName ( "ask@model:search/page:" . $page . ";key:{$key};flag:{$flag};" );
		$data = S ( $cacheName );
		if (! $data) {
			$num = 10;
			$askContextModel = M ( 'ask_context' );
			$askUploadModel = M ( 'ask_upload' );
			$where = " ask_title like '%{$key}%' or ask_context like '%{$key}%' ";
			if ($flag == - 1) {
				$where .= " and ask_flag=-1 ";
			} elseif ($flag == 0) {
				$where .= " and ask_flag=0 ";
			} else {
				$where .= " and ask_flag=1";
			}
			$data ['item'] = $askContextModel->where ( $where )->order ( 'ask_sort desc, ask_id desc' )->page ( $page . ',' . $num )->findAll ();
			// 获取图片
			foreach ( ( array ) $data ['item'] as $key => $val ) {
				$upload = $askUploadModel->where ( 'context_id=' . intval ( $val ['ask_id'] ) )->find ();
				if ($upload ['upload_name']) {
					$data ['item'] [$key] ['thumbName'] = $upload ['upload_name'];
					// 获取缩略图
					$data ['item'] [$key] ['thumb'] = $this->_thumb ( $upload ['upload_path'], $upload ['upload_name'] );
				}
				$commentModel = M ( 'comment' );
				$where1 = "appid=" . intval ( $val ['ask_id'] );
				$data ['item'] [$key] ['ask_num'] = $commentModel->where ( $where1 )->count ();
			}
			$data ['total'] = $askContextModel->where ( $where )->count ();
			// 分页
			import ( "@.ORG.Page" );
			// $page = new AskPage ( $data ['total'], $num );
			$page = new page ( $data ['total'], $num );
			$data ['pageShow'] = $page->show ();
			S ( $cacheName, $data, C ( 'cache_time_mini' ) );
		}
		return $data;
	}
	
	function listAnswer($uid = '') {
		$page = intval ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		
		$cacheName = getCacheName ( "ask@answerList/page:" . $page . ";uid:{$uid};" );
		$data = S ( $cacheName );
		if (! $data) {
			$num = 10;
			$commentModel = M ( 'comment' );
			$where = 'type="ask" and uid="' . intval ( $uid ) . '"';
			$data ['item'] = $commentModel->where ( $where )->order ( '`id` DESC ' )->page ( $page . ',' . $num )->findAll ();
			$data ['total'] = $commentModel->where ( $where )->count ();
			
			// 分页
			import ( "@.ORG.Page" );
			// $page = new AskPage ( $data ['total'], $num );
			$page = new page ( $data ['total'], $num );
			$data ['pageShow'] = $page->show ();
			S ( $cacheName, $data, C ( 'cache_time_mini' ) );
		}
		return $data;
	}
	
	function getContext($contextId) {
		$page = intval ( $_POST ['p'] ) ? $_POST ['p'] : 1;
		$cacheName = getCacheName ( "ask@context/id:" . $contextId . ";" );
		$data = S ( $cacheName );
		if (! $data) {
			$askContextModel = M ( 'ask_context' );
			$askUploadModel = M ( 'ask_upload' );
			$data = $askContextModel->find ( $contextId );
			$data ['uploads'] = $askUploadModel->where ( 'context_id=' . $contextId )->findAll ();
			
			// 获取图片
			foreach ( ( array ) $data ['uploads'] as $key => $val ) {
				// 获取缩略图
				$data ['uploads'] [$key] ['thumb'] = $this->_thumb ( $val ['upload_path'], $val ['upload_name'], 400, 400 );
			}
			S ( $cacheName, $data, C ( 'cache_time_short' ) );
		}
		return $data;
	}
	
	// 缩略图
	private function _thumb($path, $name = '', $width = 120, $height = 160) {
		$thumbPath = SITE_PATH . '/up_files/thumb/' . str_replace ( '.', "_{$width}_{$height}.", $path );
		$imagePath = SITE_PATH . '/data/uploads/' . $path;
		$url = '/up_files/thumb/' . str_replace ( '.', "_{$width}_{$height}.", $path );
		
		if (! file_exists ( $thumbPath )) {
			// 去除文件名,获取路径
			$_filepath = explode ( '/', $thumbPath );
			$_count = count ( $_filepath );
			$filePath = '';
			foreach ( ( array ) $_filepath as $k => $v ) {
				if ($k == ($_count - 1)) {
				
				} else {
					$filePath .= $v . '/';
				}
			}
			makedir ( $filePath );
			$rs = makeThumb ( $imagePath, $thumbPath, $width, $height );
		}
		
		$thum = "<img alt='" . $name . "' src='" . $url . "' onError='error($(this))' />";
		return $thum;
	}
	
	function listComment($id) {
		$num = 10;
		$page = intval ( $_POST ['p'] ) ? $_POST ['p'] : 1;
		$cacheName = getCacheName ( "ask@comment/id:" . $id . ";page:{$page};" );
		$data = S ( $cacheName );
		if (! $data) {
			$num = 10;
			$commentModel = M ( 'comment' );
			$where = "appid=" . intval ( $id );
			$data ['item'] = $commentModel->where ( $where )->order ( '' )->page ( $page . ',' . $num )->findAll ();
			
			$data ['total'] = $commentModel->where ( $where )->count ();
			
			// 分页
			import ( "@.ORG.Page" );
			// $page = new AskPage ( $data ['total'], $num );
			$page = new page ( $data ['total'], $num );
			$data ['pageShow'] = $page->show ();
			S ( $cacheName, $data, C ( 'cache_time_mini' ) );
		}
		return $data;
	}
	
	function addWeibo($title, $url, $uid, $context = '', $typeData = '') {
		if (! intval ( $uid )) {
			return false;
		}
		$weiboModel = D ( 'weibo' );
		$data = array ();
		$data ['uid'] = $uid;
		$data ['content'] = $context ? $context : $title;
		$data ['ctime'] = time ();
		$data ['from_data'] = serialize ( array ('app_type' => 'local_app', 'app_name' => 'ask', 'title' => $title, 'url' => $url ) );
		if ($typeData) {
			$data ['type_data'] = serialize ( $typeData );
			$data ['type'] = 1;
		}
		$data ['sort'] = 0;
		return $weiboModel->add ( $data );
	}
}
?>